/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */

import UIKit
import SnapKit
import SDWebImage

class LSWelComeViewController: UIViewController {
    
    //MARK: - 懒加载
    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //修改圆角
        imageView.layer.cornerRadius = 45
        //剪切掉多余的
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    //欢迎信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        //如果有头像,我们服务端提供的头像avatar_large
        if let avatar_large = LSUserAccountViewModel.sharedUserAccount.userAccount?.avatar_large {
            
            self.headImageView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        if let name = LSUserAccountViewModel.sharedUserAccount.userAccount?.name {
            label.text = "欢迎回来, \(name)"
        } else {
            label.text = "欢迎回来"
        }
        
        return label
    }()
    
    override func loadView() {
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    //MARK: - 页面显示出来,执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //MARK: - 执行动画
    private func startAnimation() {
        //更新约束
        //设置透明度
        messageLabel.alpha = 0
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        //usingSpringWithDamping,表示阻尼,0-1,阻尼越大表示弹簧效果越小
        //initialSpringVelocity 表示弹簧初始速度
        //枚举什么不传,可以使用中括号,多个枚举使用逗号隔开
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
        }

    }
    
    //MARK: - 添加视图,设置约束
    private func setupUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 90, height: 90))
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }
    }
    
}

















